<?php
      class cyz_calender_route
      {
      	 public $calender;
      	 public $dao;
      	 public $route_id;
      	 
      	 function __construct($route_id,$date='')
      	 {
      	 	$this->calender = new cyz_calender($date);
      	 	$this->dao = new TTable('jsy_route_date');
      	 	$this->route_id = $route_id;
      	 }
      	 /**
      	  * 获取指定月的日历
      	  */
      	 function get_month()
      	 {
      	 	$data = array();
      	 	if (!$this->route_id) return $data;
      	 	$data = $this->calender->get_month();
      	 	return $this->route_date($data);
      	 }
     	 /**
      	  * 下个月的日历
      	  */
      	function get_next_month()
    	{
    		$data = array();
      	 	if (!$this->route_id) return $data;
      	 	$data = $this->calender->get_next_month();
      	 	return $this->route_date($data);
    	}
    	/**
    	 * 上个月的日历
    	 */
    	function get_last_month()
    	{
    		$data = array();
      	 	if (!$this->route_id) return $data;
      	 	$data = $this->calender->get_last_month();
      	 	return $this->route_date($data);
    	}
        /**
         * 线路团期
         * @param $data
         */
      	 function route_date(&$data)
      	 {	
      	 	$start_date = $this->calender->year.'-'.$this->calender->month.'-01 00:00:00';
      	 	$end_date = $this->getdate($start_date,$this->calender->days_num-1);
      	 	$start_date = date("Y-m-d",strtotime($start_date));
      	 	
      	 	$fields = '*,DAYOFMONTH(route_date) as days';
      	 	$where = array(
      	 	           array('route_id', $this->route_id),
      	 	           array('route_date', $start_date, '>='),
      	 	           array('route_date', $end_date, '<='),
      	 	           array('status', 0),
      	 	           array('mark', 0),
      	 	       );

      	 	$query = $this->dao->get($fields,$where);
      	 	if ($query)
      	 	{
      	 		foreach ($query as $key=>$route)
      	 		{
      	 			$days = $route['days'];
      	 			if (isset($data[$days]))
      	 			$data[$days] = array_merge($data[$days],$route);
      	 		}
      	 	}
      	 	
      	 	return $data;
      	 	
      	 }
      	 /**
      	  * 返回几天后的日期
      	  * @param unknown_type $start_date
      	  * @param unknown_type $days
      	  */
      	 function getdate($start_date,$days)
      	 {
      	 	$start_date = strtotime($start_date) + $days * 24 * 3600;
      	 	return date("Y-m-d",$start_date);
      	 }
      	 
      	 /**
      	  * 返回可够日历调用的数组
      	  */
      	 function getCalenderDate(&$data)
      	 {
      	 	$b = array();
      	 	$e = array();
      	 	$num = $data[1][1];
      	 	for ($i=0;$i<$num;$i++)
      	 	{
      	 		$b[-$num+$i] = array(0,$i,$data[1][2]);
      	 	}
      	 	$days = count($data);
      	 	$num = $data[$days][1];
      	 	$key = $data[$days][2];
      	 	
      	 	$num2 = 42 - $data[1][1] - $days;
      		 for ($i=1;$i<=$num2;$i++)
      	 	{
      	 		$week = $num + $i ;
      	 		$week = $week%7;
      	 		if ($week == 0)
      	 		{
      	 			$key ++;
      	 		}
      	 		$e[$days+$i] = array(0,$week,$key);
      	 	}
      	 	$data = array_merge($b,$data,$e);
      	 	return $data;
      	 }
      	 
      }